//
//  RadioPlayHomeView.m
//  SunXi
//
//  Created by dllo on 15/11/29.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "RadioPlayHomeView.h"

@implementation RadioPlayHomeView{
    UIImageView *_myimageView;
    UISlider *_slider;
    UILabel *_label;
    NSTimer *_time;
    UILabel *_nowlabel;
    UILabel *_Alllabel;
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self createView];
    }
    return self;
}

- (void)createView{
    self.backgroundColor = [UIColor whiteColor];
    _slider = [[UISlider alloc]init];
    [self addSubview:_slider];
    [_slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.frame.size.width - 80);
        make.bottom.equalTo(self).offset(-120);
        make.height.mas_equalTo(20);
        make.centerX.equalTo(self);
    }];
    UIImage *ii = [UIImage imageNamed:@"shugang"];
//
    [_slider setThumbImage:ii forState:UIControlStateHighlighted];
    [_slider setThumbImage:ii forState:UIControlStateNormal];
    _slider.minimumTrackTintColor = [UIColor colorWithRed:0.531 green:0.761 blue:0.103 alpha:1.000];
//    _slider.maximumTrackTintColor = [UIColor clearColor];
    [_slider addTarget:self action:@selector(huadong:) forControlEvents:UIControlEventValueChanged];
    _slider.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [_slider addGestureRecognizer:tap];

    _nowlabel = [[UILabel alloc]init];
    [self addSubview:_nowlabel];
    [_nowlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_slider);
        make.bottom.equalTo(_slider.mas_top).offset(-2);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(40);
    }];
    _nowlabel.font = [UIFont systemFontOfSize:13];
    _nowlabel.text = @"00:00";
    
    
    _Alllabel = [[UILabel alloc]init];
    [self addSubview:_Alllabel];
    [_Alllabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_slider);
        make.bottom.equalTo(_slider.mas_top).offset(-2);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(40);
    }];
    _Alllabel.font = [UIFont systemFontOfSize:13];
    _Alllabel.text = @"00:00";
    
    
    
    _label = [[UILabel alloc]init];
    [self addSubview:_label];
    [_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.frame.size.width);
        make.height.mas_equalTo(20);
        make.centerX.equalTo(self);
        make.bottom.equalTo(_slider.mas_top).offset(-30);
    }];
    _label.textAlignment = NSTextAlignmentCenter;
    
    
    CGFloat hei;
    if (SWIDTH == 320) {
        hei = 20;
    }else if (SWIDTH > 400){
        hei = 80;
    }else{
        hei = 60;
    }
    
//
    _myimageView = [[UIImageView alloc]init];
    [self addSubview:_myimageView];
    [_myimageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.centerX.equalTo(self);
        make.width.equalTo(_myimageView.mas_height);
        make.bottom.equalTo(_label.mas_top).offset(- hei);

    }];
    
    
    UIButton *sharbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self addSubview:sharbutton];
    [sharbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_slider);

        make.bottom.equalTo(_slider.mas_bottom).offset(35);
        make.height.mas_equalTo(25);
        make.width.mas_equalTo(25);
        
    }];
    [sharbutton setTintColor:[UIColor blackColor]];
    [sharbutton setImage:[UIImage imageNamed:@"fenxiang"] forState:0];
    [sharbutton addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    
//    /**
//     *  下载按钮
//     */
//    UIButton *downbutton = [UIButton buttonWithType:UIButtonTypeSystem];
//    [self addSubview:downbutton];
//    [downbutton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(_slider).offset(20);
//        make.bottom.equalTo(_slider.mas_bottom).offset(35);
//        make.height.mas_equalTo(25);
//        make.width.mas_equalTo(25);
//        
//    }];
//    [downbutton setTintColor:[UIColor blackColor]];
//    [downbutton setImage:[UIImage imageNamed:@"xz"] forState:0];
//    [downbutton addTarget:self action:@selector(down) forControlEvents:UIControlEventTouchUpInside];
//
//    
    
    [self pushMusic];
    if (!_time) {
        _time = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(ChangTime) userInfo:nil repeats:YES];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushMusic) name:@"更换音乐" object:nil];
    
}

- (void)share{
    RadioModel *model = [[RadioSavemodel shareSingleton].playlistArr objectAtIndex:[RadioSavemodel shareSingleton].playNumber];
    
    NSString *codeStr = [NSString stringWithFormat:@"url=%@&user=%@&radio=%@&image=%@",model.musicUrl,model.userinfo[@"userinfo"][@"uname"],model.radioid,model.coverimg];
        [shareModel sharedInstance].title = model.title;
        [shareModel sharedInstance].type = Music;
        [shareModel sharedInstance].code = codeStr;
        [[ShareWindow sharedInstance] show];
    

}
- (void)pushMusic{
    [_myimageView sd_setImageWithURL:[NSURL URLWithString:[[[RadioSavemodel shareSingleton].playlistArr objectAtIndex:[RadioSavemodel shareSingleton].playNumber] coverimg]]placeholderImage:[UIImage imageNamed:@"zan"]];
    
    _label.text = [[[RadioSavemodel shareSingleton].playlistArr objectAtIndex:[RadioSavemodel shareSingleton].playNumber] title];
    
}

- (void)ChangTime{
    _slider.value = CMTimeGetSeconds([myplay sharePlayer].currentItem.currentTime) / CMTimeGetSeconds([myplay sharePlayer].currentItem.duration);
    //
    NSInteger totalMin = CMTimeGetSeconds([myplay sharePlayer].currentItem.duration) / 60;
    NSInteger totalSecond = CMTimeGetSeconds([myplay sharePlayer].currentItem.duration) - 60 * totalMin;
    NSInteger min = CMTimeGetSeconds([myplay sharePlayer].currentItem.currentTime) / 60;
    NSInteger second = CMTimeGetSeconds([myplay sharePlayer].currentItem.currentTime) - min * 60;
    
    if (CMTimeGetSeconds([myplay sharePlayer].currentItem.currentTime) > 0) {
        _Alllabel.text = [NSString stringWithFormat:@"%02ld:%02ld",(long)totalMin,(long)totalSecond];
       
        _nowlabel.text = [NSString stringWithFormat:@"%02ld:%02ld",(long)min,(long)second];
        
    }
    
    
}
//MARK:点击跳转进度
- (void)tapAction:(UITapGestureRecognizer *)tap{
    
    CGPoint p = [tap locationInView:tap.view];
    CGFloat touch = p.x / (self.frame.size.width - 80);
    float current = touch * CMTimeGetSeconds([myplay sharePlayer].currentItem.duration);
    [[myplay sharePlayer] seekToTime:CMTimeMakeWithSeconds(current, 1000000)];
    
}
//滑动跳转的方法
- (void)huadong:(UISlider *)slider{
   
    float current = _slider.value * CMTimeGetSeconds([myplay sharePlayer].currentItem.duration);
    [[myplay sharePlayer] seekToTime:CMTimeMakeWithSeconds(current, 1000000)];
    
}
@end
